﻿
namespace HostsUpdater {

    public class Util {

        /// <summary>
        /// html实体转标记
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string EntityToHtml(string source) {
            if (string.IsNullOrEmpty(source)) {
                return string.Empty;
            }
            string result = source.Replace("&quot;", "\""); // "
            result = result.Replace("&apos;", "'"); // '--> &apos
            result = result.Replace("&lt;", "<"); // <
            result = result.Replace("&gt;", ">"); // >
            result = result.Replace("&amp;", "&"); // &
            result = result.Replace("&nbsp;", " "); // 空格
            return result;
        }


        /// <summary>
        /// html标记转实体
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string HtmlToEntity(string source) {
            if (string.IsNullOrEmpty(source)) {
                return string.Empty;
            }
            string result = source.Replace("\"", "&quot;"); // "
            result = result.Replace("'", "&apos;"); // '--> &apos;
            result = result.Replace("<", "<"); // <
            result = result.Replace(">", ">"); // >
            result = result.Replace("&", "&"); // &
            result = result.Replace(" ", "&nbsp;"); // 空格
            return result;
        }

        /// <summary>
        /// 去空格
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string TrimAll(string source) {
            if (string.IsNullOrEmpty(source)) {
                return string.Empty;
            }
            string result = source.Replace(" ", "");
            return result;
        }
    }
}
